﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;
using System.Web.Services.Protocols;

namespace CrmContrib.Workflow.Activities.Cases
{
    [CrmWorkflowActivity("Add Note to existing case", "CrmContrib \\ Cases")]
    public class AddNoteToExistingCaseActivity : Activity
    {
        [CrmInput("CaseId")]
        [ValidationOption(ValidationOption.Required)]
        public String CaseId
        {
            get { return (String)base.GetValue(CaseIdProperty); }
            set { base.SetValue(CaseIdProperty, value); }
        }

        public static readonly DependencyProperty CaseIdProperty = DependencyProperty.Register("CaseId", typeof(String), typeof(AddNoteToExistingCaseActivity));


        [CrmInput("Note")]
        [ValidationOption(ValidationOption.Required)]
        public string Note
        {
            get { return (string)base.GetValue(NoteProperty); }
            set { base.SetValue(NoteProperty, value); }
        }

        public static readonly DependencyProperty NoteProperty = DependencyProperty.Register("Note", typeof(string), typeof(AddNoteToExistingCaseActivity));

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            var contextService = executionContext.GetService<IContextService>();
            var workflowContext = contextService.Context;
            var crmService = workflowContext.CreateCrmService();

            incident theIncident = (incident)crmService.Retrieve("incident", new Guid(CaseId), new AllColumns());

            if (theIncident != null)
            {
                annotation annotation = new annotation();
                annotation.objectid = new Lookup();
                annotation.objectid.Value = theIncident.incidentid.Value;
                annotation.objectid.type = EntityName.incident.ToString();
                annotation.objecttypecode = new EntityNameReference();
                annotation.objecttypecode.Value = EntityName.incident.ToString();
                annotation.ownerid = new Owner();
                annotation.ownerid.type = theIncident.ownerid.type;
                annotation.ownerid.Value = theIncident.ownerid.Value;
                annotation.notetext = Note;

                TargetCreateAnnotation target = new TargetCreateAnnotation();
                target.Annotation = annotation;
                CreateRequest create = new CreateRequest();
                create.Target = target;
                CreateResponse created = (CreateResponse)crmService.Execute(create);
                CaseChanged = new CrmBoolean(true);

            }
            else
            {
                CaseChanged = new CrmBoolean(false);
            }



            return base.Execute(executionContext);
        }


        [CrmOutput("Has been noted")]
        public CrmBoolean CaseChanged
        {
            get { return (CrmBoolean)base.GetValue(CaseChangedProperty); }
            set { base.SetValue(CaseChangedProperty, value); }
        }

        public static readonly DependencyProperty CaseChangedProperty = DependencyProperty.Register("CaseChanged", typeof(CrmBoolean), typeof(AddNoteToExistingCaseActivity));
    }

}
